import { request } from '@/utils/request'

/**
 * 团队消息API接口
 */

// 消息状态类型（支持RabbitMQ队列状态）
export type MessageStatus = 'sending' | 'queued' | 'sent' | 'delivered' | 'failed'

// 队列信息接口
export interface QueueInfo {
  queuedAt?: number
  processedAt?: number
  retryCount: number
  routingStrategy?: string
}

// 团队消息数据类型（支持加密）
export interface TeamMessageDTO {
  id: number | string // 支持临时ID（字符串）和真实ID（数字）
  teamId: number
  senderId: number
  senderName: string
  senderAvatar?: string
  messageType: 'text' | 'image' | 'file'
  content: string
  fileName?: string
  fileSize?: number
  fileUrl?: string
  messageStatus: MessageStatus
  timestamp: number
  queueInfo?: QueueInfo // RabbitMQ队列信息
  // 加密相关字段
  iv?: string
  encryptionAlgorithm?: string
  keyVersion?: number
  isEncrypted?: boolean
}

// 发送消息请求类型（支持加密）
export interface SendMessageRequest {
  teamId: number
  content: string
  messageType: 'text' | 'image' | 'file'
  fileName?: string
  fileSize?: number
  fileUrl?: string
  replyToId?: number
  // 加密相关字段
  iv?: string
  encryptionAlgorithm?: string
  keyVersion?: number
  isEncrypted?: boolean
}

// 消息列表响应类型
export interface MessageListResponse {
  messages: TeamMessageDTO[]
  totalElements: number
  totalPages: number
  currentPage: number
  hasNext: boolean
  hasPrevious: boolean
}

// 搜索消息响应类型
export interface SearchMessageResponse {
  messages: TeamMessageDTO[]
  totalElements: number
  totalPages: number
  currentPage: number
  keyword: string
}

/**
 * 发送团队消息
 */
export const sendTeamMessage = (teamId: number, data: Omit<SendMessageRequest, 'teamId'>) => {
  return request<TeamMessageDTO>({
    url: `/teams/${teamId}/messages`,
    method: 'POST',
    data: {
      ...data,
      teamId
    }
  })
}

/**
 * 获取团队消息历史（分页）
 */
export const getTeamMessages = (teamId: number, page: number = 0, size: number = 20) => {
  return request<MessageListResponse>({
    url: `/teams/${teamId}/messages`,
    method: 'GET',
    params: {
      page,
      size
    }
  })
}

/**
 * 获取团队最近消息
 */
export const getRecentMessages = (teamId: number, limit: number = 10) => {
  return request<TeamMessageDTO[]>({
    url: `/teams/${teamId}/messages/recent`,
    method: 'GET',
    params: {
      limit
    }
  })
}

/**
 * 更新消息状态
 */
export const updateMessageStatus = (teamId: number, messageId: number, status: string) => {
  return request<void>({
    url: `/teams/${teamId}/messages/${messageId}/status`,
    method: 'PUT',
    data: {
      status
    }
  })
}

/**
 * 搜索团队消息
 */
export const searchTeamMessages = (
  teamId: number, 
  keyword: string, 
  page: number = 0, 
  size: number = 20
) => {
  return request<SearchMessageResponse>({
    url: `/teams/${teamId}/messages/search`,
    method: 'GET',
    params: {
      keyword,
      page,
      size
    }
  })
}

/**
 * 获取消息统计信息
 */
export const getMessageStatistics = (teamId: number) => {
  return request<{
    totalCount: number
    typeStatistics: Array<[string, number]>
  }>({
    url: `/teams/${teamId}/messages/statistics`,
    method: 'GET'
  })
}

/**
 * 批量获取消息（用于消息同步）
 */
export const getMessagesBatch = (teamId: number, messageIds: number[]) => {
  return request<TeamMessageDTO[]>({
    url: `/teams/${teamId}/messages/batch`,
    method: 'POST',
    data: {
      messageIds
    }
  })
}

/**
 * 获取指定时间之后的消息（用于增量同步）
 */
export const getMessagesAfter = (teamId: number, afterTime: number) => {
  return request<TeamMessageDTO[]>({
    url: `/teams/${teamId}/messages/after`,
    method: 'GET',
    params: {
      afterTime
    }
  })
}

/**
 * 删除消息（软删除）
 */
export const deleteMessage = (teamId: number, messageId: number) => {
  return request<void>({
    url: `/teams/${teamId}/messages/${messageId}`,
    method: 'DELETE'
  })
}

/**
 * 获取消息详情
 */
export const getMessageDetail = (teamId: number, messageId: number) => {
  return request<TeamMessageDTO>({
    url: `/teams/${teamId}/messages/${messageId}`,
    method: 'GET'
  })
}

// 导出默认对象，包含所有API方法
export default {
  sendTeamMessage,
  getTeamMessages,
  getRecentMessages,
  updateMessageStatus,
  searchTeamMessages,
  getMessageStatistics,
  getMessagesBatch,
  getMessagesAfter,
  deleteMessage,
  getMessageDetail
}
